package com.compass.doctor.ui.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.databinding.ActivityBaseListBinding
import com.compass.doctor.ui.home.adapter.LatestBuyerAdapter
import com.compass.doctor.ui.home.viewmodel.KnowledgeCenterViewModel
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 最新购买
 * Date:2023/12/24 16:06
 */
class LatestBuyerActivity : BaseVBActivity<ActivityBaseListBinding>(), OnRefreshListener,
    OnLoadMoreListener {



    private val knowledgeCenterViewModel : KnowledgeCenterViewModel by viewModels()
    private lateinit var mAdapter: LatestBuyerAdapter

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, LatestBuyerActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var mPage = 1
    override fun initView(savedInstanceState: Bundle?) {

        mAdapter = LatestBuyerAdapter(2)
        mBinding?.apply {
            tvTitleBar.setMiddleText("最新购买")
            refreshLayout?.apply {
                setEnableRefresh(true)
                setEnableLoadMore(true)
                setOnRefreshListener(this@LatestBuyerActivity)
                setOnLoadMoreListener(this@LatestBuyerActivity)
            }
            recyclerView.apply {
                layoutManager = LinearLayoutManager(  this@LatestBuyerActivity)
                val dp10 = dpToPx(10)
                addItemDecoration(NormalItemDecoration().apply {
                    setBounds( top = dp10, bottom = dp10)
                    setLastBottom(true)
                })
                adapter = mAdapter
            }
        }

    }

    override fun initData() {
        knowledgeCenterViewModel.specialLiveData.observe(this){
            if (mPage==1){
                mAdapter.setData(it)
                mBinding.refreshLayout.finishRefresh()
                mBinding.refreshLayout.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding.emptyView.visible()
                } else {
                    mBinding.emptyView.gone()
                }
            }else{
                mBinding.refreshLayout.finishLoadMore()
                if (!it.isNullOrEmpty()){
                    mAdapter.addAll(it)
                }else{
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            }
        }
        getOrderSpecial()
    }

    /**
     * 获取推荐列表数据
     */
    private fun getOrderSpecial() {
        knowledgeCenterViewModel.getOrderSpecial(mPage)
    }


    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getOrderSpecial()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getOrderSpecial()
    }
}